package com.example.majiang.controller;


import com.example.majiang.dto.PaginationDTO;
import com.example.majiang.mapper.UserMappper;
import com.example.majiang.model.User;
import com.example.majiang.service.NotificationService;
import com.example.majiang.service.QuestionService;
import com.example.majiang.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
    public class ProfileController {
        @Autowired
        private UserMappper userMapper;
        @Autowired
        private QuestionService questionService;
        @Autowired
        private UserUtil userUtil;
        @Autowired
        private NotificationService notificationService;

        @GetMapping("/profile/{action}")
        public String profile(HttpServletRequest request,
                              @PathVariable(name = "action") String action,
                              Model model,
                              @RequestParam(name = "page", defaultValue = "1") Integer page,
                              @RequestParam(name = "size", defaultValue = "5") Integer size) {
            User user = null;


            user=(User)request.getSession().getAttribute("user");

            if (user == null) {
                return "redirect:/";
            }
            if ("questions".equals(action)) {
                model.addAttribute("section", "questions");
                model.addAttribute("sectionName", "我的提问");
                PaginationDTO paginationDTO = questionService.list(user.getId(), page, size);
                model.addAttribute("pagination", paginationDTO);
            } else if ("replies".equals(action)) {
                PaginationDTO paginationDTO = notificationService.list(user.getId(), page, size);
                Integer unreadCount = notificationService.unreadCount(user.getId());
                model.addAttribute("section", "replies");
                model.addAttribute("pagination", paginationDTO);
                model.addAttribute("unreadCount", unreadCount);
                model.addAttribute("sectionName", "最新回复");
            }
            return "profile";
        }
    }

